package com.luozongyao.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;


public class JwtUtil {

    public static String getToken(int id,String name,String avatar){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_MONTH,7);
        String token = JWT.create()
                .withClaim("id", id)
                .withClaim("name", name)
                .withClaim("avatar",avatar)
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(Ivalue.SIGNATURE));
        return token;
    }
    public static DecodedJWT verifyToken(String token){
        return JWT.require(Algorithm.HMAC256(Ivalue.SIGNATURE)).build().verify(token);
    }
//    public static String[] splitToken(String token){
//        String[] parts = token.split("\\.");
//        if (parts.length == 2 && token.endsWith(".")) {
//            //Tokens with alg='none' have empty String as Signature.
//            parts = new String[]{parts[0], parts[1], ""};
//        }
//        if (parts.length != 3) {
//            throw new JWTDecodeException(String.format("The token was expected to have 3 parts, but got %s.", parts.length));
//        }
//        return parts;
//    }
//    public static JWTPayLoad parse(String payload){
//        JWTPayLoad payLoad = JSON.parseObject(payload, JWTPayLoad.class, Feature.IgnoreNotMatch);
//        return payLoad;
//    }
//    public static boolean overSevenDays(Date expire){
//        Calendar instance = Calendar.getInstance();
//        instance.setTime(expire);
//        instance.add(Calendar.DAY_OF_MONTH,7);
//        Date time = instance.getTime();
//        Date now = new Date();
//        return now.after(time);
//    }
//    public static void main(String[] args) {
//        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibGlzaSIsImlkIjoiMTIiLCJhdmF0YXIiOiIiLCJleHAiOjE2MzUwNzYzNDF9.ceeyHSaZbHD2dI7JYAStxQV4okp14GUEVn_KOJPOaps";
//        try {
//            JwtUtil.verifyToken(token);
//        }catch (TokenExpiredException e){
//            String payload = JwtUtil.splitToken(token)[1];
//            String json = new String(Base64.getUrlDecoder().decode(payload), StandardCharsets.UTF_8);
//            JWTPayLoad parse = JwtUtil.parse(json);
//            if (overSevenDays(parse.getExp())) {
//                System.out.println("需要重新登录");
//            }else {
//                System.out.println(getToken(parse.id, parse.name, parse.avatar));
//            }
//
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

//    public static void main(String[] args) {
//        String s = splitToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi5byg5LiJIiwiaWQiOiI0NSIsImF2YXRhciI6Imh0dHA6Ly8xOTIuMTY4LjEzNC4xMTo4ODg4L2dyb3VwMS9NMDAvMDAvMDAvd0tpR0MyRjFBRTJBQXZGR0FBQmYzZHpoRnhnNjMyLnBuZyIsImV4cCI6MTYzNTY2NTIxMX0.qOkMhDurDShSaz3SKjsOfve-9rgwojhiSnoXXH1Vrvo")[1];
//        String json = new String(Base64.getUrlDecoder().decode(s), StandardCharsets.UTF_8);
//        System.out.println(json);
//        JWTPayLoad parse = parse(json);
//        System.out.println(parse);
//
//    }


}
